﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MUD.Driver.Log
{
    /// <summary>
    /// Logging singleton.
    /// </summary>
    public static class DriverLog
    {
        /// <summary>
        /// Desired logfile path, relative to the executable.
        /// </summary>
        public static string LogfilePath = "";

        private static bool IsInit = false;

        public enum LogType : byte
        {
            Info,
            Succeed,
            Fail,
            Debug,
            Variable,
            Warning,
            Error,
            Break,
            Label
        }

        static DriverLog()
        {

        }

        public static void Initialize()
        {
            if (IsInit)
                return;
            StreamWriter swr = File.CreateText(LogfilePath);
            swr.AutoFlush = true;
            swr.Write(LogConstants.LOG_HEADER);
            swr.Flush();
            swr.Close();
            IsInit = true;
        }

        /// <summary>
        /// Appends a line to the logfile.
        /// </summary>
        /// <param name="message">String message.</param>
        /// <param name="type">Log event type.</param>
        public static void WriteLine(string message, LogType type)
        {
            string totalMessage = "";
            totalMessage += DateTime.Now.ToString("hh:mm:ss.ffff") + " - ";
            totalMessage += message;
            switch (type)
            {
                case LogType.Info:
                    File.AppendAllText(LogfilePath, String.Format(LogConstants.INFO_STRING, totalMessage));
                    break;
                case LogType.Succeed:
                    File.AppendAllText(LogfilePath, String.Format(LogConstants.SUCCEED_STRING, totalMessage));
                    break;
                case LogType.Fail:
                    File.AppendAllText(LogfilePath, String.Format(LogConstants.FAIL_STRING, totalMessage));
                    break;
                case LogType.Debug:
                    File.AppendAllText(LogfilePath, String.Format(LogConstants.DEBUG_STRING, totalMessage));
                    break;
                case LogType.Variable:
                    File.AppendAllText(LogfilePath, String.Format(LogConstants.VARIABLE_STRING, totalMessage));
                    break;
                case LogType.Warning:
                    File.AppendAllText(LogfilePath, String.Format(LogConstants.WARNING_STRING, totalMessage));
                    break;
                case LogType.Error:
                    File.AppendAllText(LogfilePath, String.Format(LogConstants.ERROR_STRING, totalMessage));
                    break;
                case LogType.Label:
                    File.AppendAllText(LogfilePath, String.Format(LogConstants.LABEL_STRING, totalMessage));
                    break;
            }
        }

        /// <summary>
        /// Appends a line to the logfile. Assumes "info" log event type.
        /// </summary>
        /// <param name="message">String message to append.</param>
        public static void WriteLine(string message)
        {
            string totalMessage = "";
            totalMessage += DateTime.Now.ToString("hh:mm:ss.ffff") + " - ";
            totalMessage += message;
            File.AppendAllText(LogfilePath, String.Format(LogConstants.INFO_STRING, totalMessage));
        }

        public static void WriteBreak()
        {
            File.AppendAllText(LogfilePath, String.Format(LogConstants.BREAK_STRING, ""));
        }

    }
}
